/*
  学习目标：使用Actioncreator创建Action
  步骤：
*/

import { useState } from 'react';
import { useDispatch } from 'react-redux';
import { useSelector } from 'react-redux';
import { addActionCreator, desActionCreator } from './store/action';

export default function App() {
  const [show, setShow] = useState(true);
  return (
    <div>
      <h1>Redux基础案例</h1>
      <button onClick={() => setShow(!show)}>点击切换卸载挂载</button>
      {show ? <Son /> : null}
    </div>
  );
}

function Son() {
  const count = useSelector((state) => state.count);

  const dispatch = useDispatch();

  return (
    <div>
      <h2>子组件</h2>
      <h2>count:{count}</h2>
      {/* 2. 替换字符串，注意传参 */}
      <button onClick={() => dispatch(addActionCreator(10))}>+10</button>
      <button onClick={() => dispatch(addActionCreator(1))}>+1</button>
      <button onClick={() => dispatch(desActionCreator(10))}>-10</button>
      <button onClick={() => dispatch(desActionCreator(5))}>-5</button>
    </div>
  );
}
